package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ProcessTemplates;
import com.ruoyi.system.domain.TemplateGroup;
import com.ruoyi.system.domain.dto.json.UserInfo;
import com.ruoyi.system.mapper.ProcessTemplatesMapper;
import com.ruoyi.system.mapper.TemplateGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FormGroupsMapper;
import com.ruoyi.system.domain.FormGroups;
import com.ruoyi.system.service.IFormGroupsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 单分组Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-27
 */
@Service
public class FormGroupsServiceImpl implements IFormGroupsService
{
    @Autowired
    private FormGroupsMapper formGroupsMapper;
    @Autowired
    private TemplateGroupMapper templateGroupMapper;
    @Autowired
    private ProcessTemplatesMapper processTemplatesMapper;

    /**
     * 查询单分组
     *
     * @param groupId 单分组主键
     * @return 单分组
     */
    @Override
    public FormGroups selectFormGroupsByGroupId(Long groupId)
    {
        FormGroups formGroups = formGroupsMapper.selectFormGroupsByGroupId(groupId);
        //组装审批模板对象
        List<ProcessTemplates> ProcessTemplateList = new ArrayList();
        TemplateGroup templateGroup = new TemplateGroup();
        templateGroup.setGroupId(groupId);
        List<TemplateGroup> templateGroupList = templateGroupMapper.selectTemplateGroupList(templateGroup);
        for(TemplateGroup templateGroupVo : templateGroupList){
            String templateId = templateGroupVo.getTemplateId();
            ProcessTemplates processTemplates = processTemplatesMapper.selectProcessTemplatesByTemplateId(templateId);
            String delFlag = processTemplates.getDelFlag();
            if("1".equals(delFlag)){
                ProcessTemplateList.add(processTemplates);
            }
        }
        formGroups.setProcessTemplates(ProcessTemplateList);
        return formGroups;
    }

    /**
     * 查询单分组列表
     *
     * @param formGroups 单分组
     * @return 单分组
     */
    @Override
    public List<FormGroups> selectFormGroupsList(FormGroups formGroups)
    {
        List<FormGroups> formGroupsList = formGroupsMapper.selectFormGroupsList(formGroups);
        //组装审批模板对象
        for(FormGroups formGroup : formGroupsList){
            List<ProcessTemplates> ProcessTemplateList = new ArrayList();
            TemplateGroup templateGroup = new TemplateGroup();
            templateGroup.setGroupId(formGroup.getGroupId());
            List<TemplateGroup> templateGroupList = templateGroupMapper.selectTemplateGroupList(templateGroup);
            if(!CollectionUtils.isEmpty(templateGroupList)){
                for(TemplateGroup templateGroupVo : templateGroupList){
                    String templateId = templateGroupVo.getTemplateId();
                    ProcessTemplates processTemplates = processTemplatesMapper.selectProcessTemplatesByTemplateId(templateId);
                    processTemplates.setFormName(processTemplates.getTemplateName());
                    if(!ObjectUtils.isEmpty(processTemplates)){
                        String delFlag = processTemplates.getDelFlag();
                        if("1".equals(delFlag)){
                            ProcessTemplateList.add(processTemplates);
                        }
                    }
                }
            }
            formGroup.setProcessTemplates(ProcessTemplateList);
        }
        return formGroupsList;
    }

    /**
     * 新增单分组
     *
     * @param formGroups 单分组
     * @return 结果
     */
    @Override
    public int insertFormGroups(FormGroups formGroups)
    {
        formGroups.setCreateTime(DateUtils.getNowDate());
        FormGroups formGroupsResult = formGroupsMapper.selectFormGroupsSortMax();
        if(!ObjectUtils.isEmpty(formGroupsResult)){
            String sortNum = formGroupsResult.getSortNum();
            Integer integer = Integer.valueOf(sortNum);
            int sort = integer + 1;
            formGroups.setSortNum(String.valueOf(sort));
        }else{
            formGroups.setSortNum("1");
        }
        return formGroupsMapper.insertFormGroups(formGroups);
    }

    /**
     * 修改单分组
     *
     * @param formGroups 单分组
     * @return 结果
     */
    @Override
    public int updateFormGroups(FormGroups formGroups)
    {
        formGroups.setUpdateTime(DateUtils.getNowDate());
        return formGroupsMapper.updateFormGroups(formGroups);
    }


    /**
     * 删除单分组信息
     *
     * @param groupId 单分组主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteFormGroupsByGroupId(Long groupId)
    {
        int i = formGroupsMapper.deleteFormGroupsByGroupId(groupId);
        //删除分组下的模型
        TemplateGroup templateGroup = new TemplateGroup();
        templateGroup.setGroupId(groupId);
        List<TemplateGroup> templateGroups = templateGroupMapper.selectTemplateGroupList(templateGroup);
        for(TemplateGroup templateGroupVo : templateGroups){
            Long id = templateGroupVo.getId();
            templateGroupMapper.deleteTemplateGroupById(id);
        }
        return i;
    }


    /**
     * 查询单分组列表
     *
     * @param sysuser 用户
     * @return 单分组集合
     */
    @Override
    public List<FormGroups> selectFormGroupsListByUserId(SysUser sysuser){
        Long deptId = sysuser.getDeptId();
        Long userId = sysuser.getUserId();
        FormGroups formGroups = new FormGroups();
        List<FormGroups> formGroupsList = formGroupsMapper.selectFormGroupsList(formGroups);
        //组装审批模板对象
        for(FormGroups formGroup : formGroupsList){
            List<ProcessTemplates> ProcessTemplateList = new ArrayList();
            TemplateGroup templateGroup = new TemplateGroup();
            templateGroup.setGroupId(formGroup.getGroupId());
            List<TemplateGroup> templateGroupList = templateGroupMapper.selectTemplateGroupList(templateGroup);
            for(TemplateGroup templateGroupVo : templateGroupList){
                String templateId = templateGroupVo.getTemplateId();
                ProcessTemplates processTemplates = processTemplatesMapper.selectProcessTemplatesByTemplateId(templateId);
                String isStop = processTemplates.getIsStop();
                //停用
                if("0".equals(isStop)){
                    continue;
                }
                String whoCommit = processTemplates.getWhoCommit();
                List<UserInfo> userInfos = JSON.parseArray(whoCommit, UserInfo.class);
                for(UserInfo info : userInfos){
                    String type = info.getType();
                    String id = info.getId();
                    if("dept".equals(type)){
                        if(id.equals(deptId)){
                            String delFlag = processTemplates.getDelFlag();
                            if("1".equals(delFlag)){
                                ProcessTemplateList.add(processTemplates);
                            }
                        }
                    }else if("user".equals(type)){
                        if(id.equals(userId)){
                            String delFlag = processTemplates.getDelFlag();
                            if("1".equals(delFlag)){
                                ProcessTemplateList.add(processTemplates);
                            }
                        }
                    }
                }

            }
            formGroup.setProcessTemplates(ProcessTemplateList);
        }
        return formGroupsList;
    }
}
